package com.tiger.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tiger.utils.MD5Utils;
import com.tiger.utils.UpdateUtils;
import com.tiger.mapper.base.UserMapper;
import com.tiger.pojo.base.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author HuXuehao (StudiousTiger)
 * @desc 这个类的作用是
 * @date 2022/2/8
 */
@RestController
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    HttpServletRequest httpServletRequest; //通过注解获取一个request

    @Autowired
    private UpdateUtils updateUtils;

    /**
     * 根据UID注销用户
     * @param UID
     * @return
     */
    @RequestMapping(value = "/user/log_off/{UID}", name = "根据UID注销用户/user")
    public String logOffUser(@PathVariable String UID){
        User user = new User();
        user.setUserId(UID);
        user.setUserTag(1); // 1表示注销
        int res = userMapper.updateById(user);
        return null;
    }

    /**
     * 修改用户头像
     * @param headPhotoPath
     * @return
     */
    @RequestMapping(value = "/user/update/head_photo/{headPhotoPath}", name = "修改用户头像/user")
    public String updateUserHeadPhotoPath(@PathVariable String headPhotoPath){
        User user = new User();
        user.setUserId((String)httpServletRequest.getSession().getAttribute("userId"));
        user.setUserHeadPortrait(headPhotoPath);
        int res = userMapper.updateById(user);
        return null;
    }

    /**
     * 获取昵称
     * @return
     */
    @RequestMapping(value = "/user/get/nickname", name = "获取昵称/user")
    public String getUserNickname(){
        String userId = (String) httpServletRequest.getSession().getAttribute("userId");
        if (userId != null){
            try {
                User user = userMapper.selectById(userId);
                return "".equals(user.getUserNickname())? "暂无":user.getUserNickname();
            }catch (Exception e){
                return "error";
            }
        }else{
            return "error";
        }
    }
    /**
     * 修改昵称
     * @param nickName
     * @return
     */
    @RequestMapping(value = "/user/update/nickname/{nickName}", name = "修改昵称/user")
    public String updateUserNickname(@PathVariable String nickName){
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",(String)httpServletRequest.getSession().getAttribute("userId"));
        wrapper.set("user_nickname", nickName);
        int res = userMapper.update(null, wrapper);
        if (res==1){
            return "true";
        }else {
            return "false";
        }
    }

    /**
     * 获取性别
     * @return
     */
    @RequestMapping(value = "/user/get/gender", name = "获取性别/user")
    public String getGender(){
        String userId = (String) httpServletRequest.getSession().getAttribute("userId");
        if (userId != null){
            try {
                User user = userMapper.selectById(userId);
                return user.getUserGender();
            }catch (Exception e){
                return "error";
            }
        }else{
            return "error";
        }
    }

    /**
     * 更新用户性别
     * @param gender
     * @return
     */
    @RequestMapping(value = "/user/update/gender/{gender}", name = "更新用户性别/user")
    public String updateGender(@PathVariable String gender){
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",(String)httpServletRequest.getSession().getAttribute("userId"));
        wrapper.set("user_gender", gender);
        int res = userMapper.update(null, wrapper);
        if (res==1){
            return "true";
        }else {
            return "false";
        }
    }

    /**
     * 修改密码
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @RequestMapping(value = "/user/update/password/{oldPassword}/{newPassword}", name = "修改密码/user")
    public String updateUserPassword(@PathVariable String oldPassword,@PathVariable String newPassword){
        String userId = (String) httpServletRequest.getSession().getAttribute("userId");
        User user = userMapper.selectById(userId);
        if (MD5Utils.string2MD5(oldPassword).equals(user.getUserPassword())){
            UpdateWrapper<User> wrapper = new UpdateWrapper<>();
            wrapper.eq("user_id",userId);
            wrapper.set("user_password", MD5Utils.string2MD5(newPassword));
            int res = userMapper.update(null, wrapper);
            if (res==1){
                return "true";
            }else {
                return "false";
            }
        }
        return "error";

    }

    /**
     * 修改邮箱
     * @param email
     * @return
     */
    @RequestMapping(value = "/user/update/email/{email}", name = "修改邮箱/user")
    public String updateUserEmail(@PathVariable String email){
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",(String)httpServletRequest.getSession().getAttribute("userId"));
        wrapper.set("user_email", MD5Utils.string2MD5(email));
        int res = userMapper.update(null, wrapper);
        if (res==1){
            return "true";
        }else {
            return "false";
        }
    }

    /**
     * 获取签名
     * @return
     */
    @RequestMapping(value = "/user/get/sign", name = "获取签名/user")
    public String getSign(){
        String userId = (String) httpServletRequest.getSession().getAttribute("userId");
        if (userId != null){
            try {
                User user = userMapper.selectById(userId);
                return "".equals(user.getUserSign())? "暂无":user.getUserSign();
            }catch (Exception e){
                return "error";
            }
        }else{
            return "noLogin";
        }
    }
    /**
     * 修改用户签名
     * @param sign
     * @return
     */
    @RequestMapping(value = "/user/update/sign/{sign}", name = "修改用户签名/user")
    public String updateUserSign(@PathVariable String sign){
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id",(String)httpServletRequest.getSession().getAttribute("userId"));
        wrapper.set("user_sign",sign);
        int res = userMapper.update(null, wrapper);
        if (res==1){
            return "true";
        }else {
            return "false";
        }
    }

    /**
     * 根据UID查询用户
     * @return
     */
    @RequestMapping(value = "/user/query", name = "根据UID查询用户/user")
    public String queryUser(){
        String userId = (String) httpServletRequest.getSession().getAttribute("userId");
        if (userId==null){
            return "noLogin";
        }
        try {
            User user = userMapper.selectById(userId);
            if (user.getUserId()!=null){
                user.setUserHeadPortrait(updateUtils.getNewHeadPhotoURL(user.getUserHeadPortrait()));
                return JSON.toJSONString(user);
            } else {
                return "false";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "false";
        }
    }

    /**
     * 根据UID查询用户
     * @return
     */
    @RequestMapping(value = "/user/queryByID/{userId}", name = "根据UID查询用户/user")
    public String queryUserById(@PathVariable String userId){
        try {
            User user = userMapper.selectById(userId);
            if (user.getUserId()!=null){
                user.setUserHeadPortrait(updateUtils.getNewHeadPhotoURL(user.getUserHeadPortrait()));
                return JSON.toJSONString(user);
            } else {
                return "false";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "false";
        }

    }

    /**
     * 获取当前登录用户的ID
     * @return
     */
    @RequestMapping(value = "/user/myselfID", name = "获取当前登录用户的ID/user")
    public String getMyselfID(){
        try {
            String userId = (String) httpServletRequest.getSession().getAttribute("userId");
            User user = userMapper.selectById(userId);
            return JSON.toJSONString(user);
        } catch (Exception e) {
            return "false";
        }

    }


    @RequestMapping(value = "/user/getUserInfoById/{UID}", name = "根据id获取用户信息/user")
    public String getUserInfoById(@PathVariable String UID){
        try {
            User user = userMapper.selectById(UID);
            if (user != null) {
                user.setUserHeadPortrait(
                        updateUtils.getNewHeadPhotoURL(
                                user.getUserHeadPortrait()
                        )
                );
                return JSON.toJSONString(user);
            } else {
                return "false";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "false";
        }
    }

    @RequestMapping(value = "/user/getCurrentUserInfo", name = "获取当前登录用户的信息/user")
    public String getCurrentUserInfo(){
        try {
            User user = userMapper.selectById((String) httpServletRequest.getSession().getAttribute("userId"));
            if (user != null) {
                user.setUserHeadPortrait(
                        updateUtils.getNewHeadPhotoURL(
                                user.getUserHeadPortrait()
                        )
                );
                return JSON.toJSONString(user);
            } else {
                return "false";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "false";
        }
    }
}
